<template>
    <div>
        <!-- 搜索组件 -->
        <search-form :items="formItems" :params="params" :on-submit="onSearch" :btn-loading="loading" />

        <!-- 表格区域 -->
        <div class="table-body">
            <!-- 左上角按钮 -->
            <div class="table-right-top">
            </div>

            <!-- 表格 -->
            <el-table border style="width: 100%;" :data="data" v-loading="loading">
                <el-table-column align="center" prop="order_no" :label="$i18n.t('ORDER_NO')" :min-width="150" fixed="left"></el-table-column>
                <el-table-column align="center" prop="product_name" :label="$i18n.t('PRODUCT_NAME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="risk_name" :label="$i18n.t('RISK_NAME')" :min-width="150"></el-table-column>
                <el-table-column align="center" prop="loan_type" :label="$i18n.t('LOAN_TYPE')" :min-width="130"></el-table-column>
                <el-table-column align="center" prop="result" :label="$i18n.t('RISK_RESULT')" :min-width="100">
                    <template slot-scope="scope">
                        <el-tag size="mini" v-if="scope.row.result === 0" effect="dark">{{ scope.row.result_text }}</el-tag>
                        <el-tag size="mini" v-if="scope.row.result === 1" type="success" effect="dark">{{ scope.row.result_text }}</el-tag>
                        <el-tag size="mini" v-if="scope.row.result === 2" type="danger" effect="dark">{{ scope.row.result_text }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="score" :label="$i18n.t('RISK_SCORE')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="is_hit" :label="$i18n.t('IS_HIT')" :min-width="100">
                    <template slot-scope="scope">
                        {{ scope.row.is_hit ? $i18n.t('YES') : $i18n.t('NO') }}
                    </template>
                </el-table-column>
                <el-table-column align="center" prop="hand_fee" :label="$i18n.t('RISK_SERVICE_FEE')" :min-width="100"></el-table-column>
                <el-table-column align="center" prop="record_time" :label="$i18n.t('RECORD_TIME')" :min-width="150" fixed="right"></el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="table-pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next, ->, sizes"
                    :page-size.sync="page.perPage"
                    :total="page.total"
                    :page-count="page.pageCount"
                    :current-page.sync="page.currentPage"
                    :page-sizes="PAGES_SIZE"
                    @current-change="onCurrentPageChange"
                    @size-change="onPerPageChange"
                >
                </el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
    import SearchForm from '@/components/SearchForm'
    import { getRiskRecord } from '@/api/risk'
    import { PAGES_SIZE } from '@/config/constants'

    export default {
        name: 'risk',
        components: { SearchForm },
        data () {
            return {
                PAGES_SIZE,
                params: {
                    score: [null, null]
                },
                data: [],
                page: {
                    total: 0,
                    currentPage: 1,
                    perPage: PAGES_SIZE[0],
                    pageCount: 1
                },
                loading: false
            }
        },
        created () {
            this.fetch()
        },
        computed: {
            formItems () {
                const productList = this.$store.getters.options.ProductList
                const loanTypeList = this.$store.getters.options.LoanedTypeList
                const riskList = this.$store.getters.options.RiskList
                const riskResultList = this.$store.getters.options.RiskResultList
                // 构建搜索表单
                return [
                    { title: this.$i18n.t('ORDER_NO'), type: 'input', key: 'order_no' },
                    { title: this.$i18n.t('RISK_NAME'), type: 'select', key: 'risk_id', options: riskList },
                    { title: this.$i18n.t('PRODUCT_NAME'), type: 'select', key: 'product_id', options: productList },
                    { title: this.$i18n.t('LOAN_TYPE'), type: 'select', key: 'loan_type', options: loanTypeList },
                    { title: this.$i18n.t('RISK_RESULT'), type: 'select', key: 'result', options: riskResultList },
                    { title: this.$i18n.t('RISK_SCORE'), type: 'input-number-range', key: 'score' },
                    { title: this.$i18n.t('RECORD_TIME'), type: 'date-range', key: 'record_time' }
                ]
            }
        },
        methods: {
            // 搜索按钮
            onSearch () {
                this.fetch()
            },
            // 从接口拉取数据
            fetch () {
                this.loading = true
                getRiskRecord({ ...this.params, page: this.page.currentPage, perPage: this.page.perPage }).then((res) => {
                    // eslint-disable-next-line camelcase
                    const { data, last_page, total } = res.result
                    this.data = data
                    this.page.total = total
                    // eslint-disable-next-line camelcase
                    this.page.pageCount = last_page
                }).finally(() => {
                    this.loading = false
                })
            },
            // 页数切换
            onCurrentPageChange (page) {
                this.page.currentPage = page
                this.fetch()
            },
            // 每页显示数量修改
            onPerPageChange (perPage) {
                this.page.perPage = perPage
                this.fetch()
            }
        }
    }
</script>

<style lang="less" scoped>
</style>
